package com.fmdj.customer.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import com.fmdj.customer.controller.form.*;
import com.fmdj.customer.service.OrderService;
import com.fmdj.mps.controller.form.SearchOrderLocationCacheForm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Tag(name = "OrderController", description = "订单模块Web接口")
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;

    @PostMapping("/createNewOrder")
    @Operation(summary = "创建订单")
//    @SaCheckLogin  /*调用此接口时要处于登录状态*/
    public CommonResult createOrder(@Valid @RequestBody CreateOrderForm form) {

        //生产代码
//        long customerId = StpUtil.getLoginIdAsLong();
//        form.setCustomerId(customerId);

        /*        前置工作：
        注销掉登录认证的逻辑，然后开始测试。*/
//       手动测试代码
       form.setCustomerId(1134423170456465408L);

        HashMap result = orderService.createOrder(form);

        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/searchOrderStatus")
    @Operation(summary = "查询订单状态")
    @SaCheckLogin
    public CommonResult searchOrderStatus(@Valid @RequestBody SearchOrderStatusForm form) {
        long customerId = StpUtil.getLoginIdAsLong();
        form.setCustomerId(customerId);
        Integer status = orderService.searchOrderStatus(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,status);
    }

    @PostMapping("/deleteUnAcceptOrder")
    @Operation(summary = "删除未接受的订单")
    @SaCheckLogin
    public CommonResult deleteUnAcceptOrder(@Valid @RequestBody DeleteUnAcceptOrderForm form) {
        long customerId = StpUtil.getLoginIdAsLong();
        form.setCustomerId(customerId);
        String result = orderService.deleteUnAcceptOrder(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/hasCustomerCurrentOrder")
    @Operation(summary = "是否有乘客当前订单未完成或还未接收")
    @SaCheckLogin
    public CommonResult hasCustomerCurrentOrder() {
        long customerId = StpUtil.getLoginIdAsLong();
        HasCustomerCurrentOrderForm form = new HasCustomerCurrentOrderForm();
        form.setCustomerId(customerId);
        HashMap hashMap = orderService.hasCustomerCurrentOrder(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/searchOrderForMoveById")
    @Operation(summary = "查询订单信息用于司乘同显功能")
    @SaCheckLogin
    public CommonResult searchOrderForMoveById(@Valid @RequestBody SearchOrderForMoveByIdForm form) {
        long customerId = StpUtil.getLoginIdAsLong();
        form.setCustomerId(customerId);
        HashMap hashMap = orderService.searchOrderForMoveById(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }


    @PostMapping("/confirmArriveStartPlace")
    @Operation(summary = "乘客端手动确认司机已经到达")
    @SaCheckLogin
    public CommonResult confirmArriveStartPlace(@Valid @RequestBody ConfirmArriveStartPlaceForm form) {
        Boolean result = orderService.confirmArriveStartPlace(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/searchOrderById")
    @Operation(summary = "根据id查询订单信息")
    @SaCheckLogin
    public CommonResult searchOrderById(@RequestBody @Valid SearchOrderByIdForm form) {
        long customerId = StpUtil.getLoginIdAsLong();
        form.setCustomerId(customerId);
        HashMap result = orderService.searchOrderById(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/createWxPayment")
    @Operation(summary = "创建支付订单")
    @SaCheckLogin
    public CommonResult createWxPayment(@RequestBody @Valid CreateWxPaymentForm form) {
        long customerId = StpUtil.getLoginIdAsLong();
        form.setCustomerId(customerId);
        HashMap wxPaymentResult = orderService.createWxPayment(form.getOrderId(), form.getCustomerId(), form.getCustomerVoucherId(), form.getVoucherId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, wxPaymentResult);
    }

    @PostMapping("/updateOrderAboutPayment")
    @Operation(summary = "主动查询订单状态")
    @SaCheckLogin
    public CommonResult updateOrderAboutPayment(@RequestBody @Valid UpdateOrderAboutPaymentForm form){
        String result = orderService.updateOrderAboutPayment(form);
        return CommonResult.ok().put("result", result);
    }

    @PostMapping("/searchCustomerOrderByPage")
    @Operation(summary = "查询乘客关联的订单列表")
    @SaCheckLogin
    public CommonResult searchCustomerOrderByPage(@RequestBody @Valid SearchCustomerOrderByPageForm form) {
        long customerId = StpUtil.getLoginIdAsLong();
        form.setCustomerId(customerId);
        PageUtils pageUtils = orderService.searchCustomerOrderByPage(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, pageUtils);
    }
}
